#include "Timer.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include "Informant.h"

Timer::Timer()
{
	_Updating = false;
	_bCountDown = _bDoneCounting = false;
	_FPSTimer = _CurrentTime = _ElapsedTime = 0.0f;
	_Frames = 0;
}

Timer::~Timer()
{

}

/// Function:	StartTimer
/// Author:		Dennis Norton
/// Date:		05/11/12
/// Purpose:		This tells the timer to start updating from it's current time.
void Timer::StartTimer()
{
	_Updating = true;
	_bCountDown = false;
	_PrevTime = GetTickCount();
}
void Timer::ResumeTimer()
{
	_Updating = true;
}

void Timer::StartTimer(float SecsToCountDown)
{
	_Updating = _bCountDown = true;
	_bDoneCounting = false;
	_CurrentTime = SecsToCountDown;
	_PrevTime = GetTickCount();
}

/// Function:	StopTimer
/// Author:		Dennis Norton
/// Date:		05/11/12
/// Purpose:		This tells the timer to stop updating from it's current time.
void Timer::StopTimer()
{
	_Updating = false;
}

/// Function:	ResetTimer
/// Author:		Dennis Norton
/// Date:		05/11/12
/// Purpose:		This tells the timer to set it's time to 0.
void Timer::ResetTimer()
{
	_Updating = true;
	_PrevTime = GetTickCount();
	_FPSTimer = _CurrentTime = _ElapsedTime = 0.0f;
	_Frames = 0;
}

/// Function:	Update
/// Author:		Dennis Norton
/// Date:		05/11/12
/// Purpose:		This updates the timer.
void Timer::Update()
{
	if(!_Updating)
		return;

	DWORD TimeNow = GetTickCount();
	_ElapsedTime = (TimeNow - _PrevTime) * 0.001f;
	_FPSTimer += _ElapsedTime;

	if(_ElapsedTime == 0.0f)
		_ElapsedTime += 0.0001f;
	else if(_ElapsedTime > 1.0f)
		_ElapsedTime = 1.0f;

	if(!_bCountDown)
		_CurrentTime += _ElapsedTime;
	else
		_CurrentTime -= _ElapsedTime;

	++_Frames;

	if(_CurrentTime < 0.0f && _bCountDown)
		_bDoneCounting = true;

	if(_FPSTimer > 1.0f)
	{
		_FPSTimer = 0.0f;
		_FPS = _Frames;
		_Frames = 0;
	}

	_PrevTime = TimeNow;
}